/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Comunicacion;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import clients.ComListener;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author michael
 */
public class ClientCom implements Runnable{
    private ComListener listener;
    public PrintWriter out;
    public BufferedReader in;
    public static final int PORT = 1234;
    public ClientCom(ComListener list){
        listener = list;
        Socket socket;
        try {
            socket = new Socket("localhost", PORT);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
        } catch (UnknownHostException ex) {
            System.err.println("unknown host");
            System.exit(0);
        } catch (IOException ex) {
            System.err.println("io error");
            System.exit(0);
        }
        new Thread(this).start();
    }

    public void run() {
        String input;
        try {
            while ((input = in.readLine()) != null) {
                listener.receiveMsg(input);
            }
        } catch (IOException ex) {
            System.err.println("in readline error");
            System.exit(0);
        }
    }
}
